package com.auto.survey.activity.adjustment.adjustment_high

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.entity.KnownPoint
import com.auto.survey.activity.adjustment.adjustment_high.entity.UnknownPoint
import com.auto.survey.base.ICreatView
import kotlinx.coroutines.launch

class PointView(viewModel:HighAdjusMentModle, project_id:Int, activity: AppCompatActivity):ICreatView {

    private lateinit var mView: View
    private val mActivity = activity
    private val mViewModel= viewModel
    private val mProjectId = project_id
    private val mDatas = mutableListOf<Map<String, Any>>()

    private val KEY_MAP_VIEWTYPE = "KEY_MAP_VIEWTYPE"

    private val KEY_MAP_PION = "KEY_MAP_PION"
    private val KEY_MAP_PIONTYPE = "KEY_MAP_PIONTYPE"

    private val KEY_MAP_TITLE = "KEY_MAP_TITLE"

    private val KEY_MAP_TABHI = "KEY_MAP_TABHI"

    override fun creatView(context: Context, parent: ViewGroup) {
        mView = LayoutInflater.from(context).inflate(R.layout.view_project_points, parent, false)
        val ivempty = mView.findViewById<ImageView>(R.id.iv_point_empty)
        val rv_point = mView.findViewById<RecyclerView>(R.id.rv_points)
        rv_point.layoutManager = LinearLayoutManager(mActivity)
        val adapter = MyAdapter()
        rv_point.adapter = adapter

        mActivity.lifecycleScope.launch {
            mViewModel.project.observe(mActivity){
                mDatas.clear()
                ivempty.visibility= View.VISIBLE
                rv_point.visibility = View.GONE

                if (it?.knownPoints != null && it.knownPoints.size > 0) {
                    ivempty.visibility= View.GONE
                    rv_point.visibility = View.VISIBLE

                    val titl1Map = HashMap<String, Any>()
                    titl1Map.put(KEY_MAP_VIEWTYPE, 0)
                    titl1Map.put(KEY_MAP_TITLE, "已知点(${it.knownPoints.size})")
                    mDatas.add(titl1Map)

                    val knowTabMap = HashMap<String,Any>()
                    knowTabMap.put(KEY_MAP_VIEWTYPE, 1)
                    knowTabMap.put(KEY_MAP_TABHI, "高程(m)")
                    mDatas.add(knowTabMap)

                    it.knownPoints.forEach {
                        val knowData = HashMap<String,Any>()
                        knowData.put(KEY_MAP_VIEWTYPE, 2)
                        knowData.put(KEY_MAP_PION, it)
                        knowData.put(KEY_MAP_PIONTYPE, 0x11)
                        mDatas.add(knowData)
                    }
                }

                if (it?.unknownPoints != null && it.unknownPoints.size > 0) {
                    ivempty.visibility= View.GONE
                    rv_point.visibility = View.VISIBLE

                    val titl2Map = HashMap<String, Any>()
                    titl2Map.put(KEY_MAP_VIEWTYPE, 0)
                    titl2Map.put(KEY_MAP_TITLE, "未知点(${it.unknownPoints.size})")
                    mDatas.add(titl2Map)

                    val unknowTabMap = HashMap<String,Any>()
                    unknowTabMap.put(KEY_MAP_VIEWTYPE, 1)
                    unknowTabMap.put(KEY_MAP_TABHI, "近似高程")
                    mDatas.add(unknowTabMap)

                    it.unknownPoints.forEach {
                        val unknowData = HashMap<String,Any>()
                        unknowData.put(KEY_MAP_VIEWTYPE, 2)
                        unknowData.put(KEY_MAP_PION, it)
                        unknowData.put(KEY_MAP_PIONTYPE, 0x12)
                        mDatas.add(unknowData)
                    }
                }

                adapter.setItems(mDatas)
            }
        }

    }

    override fun getView(): View {
        return mView
    }

    override fun checkView(): Boolean {
        return true
    }

    override fun getTitle(): String {
        return "点列表"
    }


    inner class MyAdapter : Adapter<ViewHolder>() {

        private var mItems = mutableListOf<Map<String, Any>>()

        fun setItems(datas:MutableList<Map<String,Any>>){
            mItems.clear()
            mItems.addAll(datas)
            notifyDataSetChanged()
        }



        override fun getItemCount(): Int {
            if (mItems.size > 0) {
                return mItems.size
            }
            return 0
        }

        override fun getItemViewType(position: Int): Int {
            return  mItems[position][KEY_MAP_VIEWTYPE] as Int
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

            if (viewType == 0) {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_textview_title, parent, false)
                return TitleViewHodler(view)
            } else if (viewType == 1) {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_pointlist_title_layout, parent, false)
                return TabViewHodler(view)
            } else {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_points_layout, parent, false)
                return DataViewHodler(view)
            }
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val type = getItemViewType(position)

            if (type == 0) {
                val myhodler = holder as TitleViewHodler
                myhodler.title.text = mDatas[position][KEY_MAP_TITLE].toString()
            }

            if (type == 1) {
                val myhodler = holder as TabViewHodler
                myhodler.tab.text = mDatas[position][KEY_MAP_TABHI].toString()
            }

            if (type == 2) {
                val myhodler = holder as DataViewHodler

                if (mDatas[position][KEY_MAP_PIONTYPE] == 0x11) {
                    val know = mDatas[position][KEY_MAP_PION] as KnownPoint
                    myhodler.tv_point_name.text = know.know_point_name
                    myhodler.tv_point_high.text  = know.know_elevation.toString()
                    myhodler.tv_point_xy.text = ""
                    myhodler.tv_point_ctrl.text = "查看"
                }

                if (mDatas[position][KEY_MAP_PIONTYPE] == 0x12) {
                    val unknow = mDatas[position][KEY_MAP_PION] as UnknownPoint
                    myhodler.tv_point_name.text = unknow.unknow_point_name
                    myhodler.tv_point_high.text  = unknow.unknow_elevation.toString()
                    myhodler.tv_point_xy.text = ""
                    myhodler.tv_point_ctrl.text = "编辑"
                }

            }
        }

    }

    inner class TitleViewHodler(itemview:View) : ViewHolder(itemview) {
        val title = itemview.findViewById<TextView>(R.id.tv_title_item)
    }

    inner class TabViewHodler(itemview:View) : ViewHolder(itemview) {
        val tab = itemview.findViewById<TextView>(R.id.tv_tab_hight_point)
    }

    inner class DataViewHodler(itemview:View) : ViewHolder(itemview) {
        val tv_point_name = itemview.findViewById<TextView>(R.id.tv_point_name)
        val tv_point_high = itemview.findViewById<TextView>(R.id.tv_point_high)
        val tv_point_xy = itemview.findViewById<TextView>(R.id.tv_point_xy)
        val tv_point_ctrl = itemview.findViewById<TextView>(R.id.tv_point_ctrl)
    }
}